package com.sibu.yx.promotion.persistence.support.sharding.algorithm;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.shardingsphere.api.sharding.complex.ComplexKeysShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.complex.ComplexKeysShardingValue;

/**
 * 分片键参数都是字符串
 * @Description:
 * @Author yuwenbo@wljs.com
 * @Date 2020-06-03
 * @Version V1.0
 **/
public abstract class AbstractComplexShardingAlgorithm implements ComplexKeysShardingAlgorithm<String> {
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, ComplexKeysShardingValue<String> shardingValue) {
        Set<String> dsIndex = new HashSet<>();
        shardingValue.getColumnNameAndShardingValuesMap().forEach((columnName, collectionValue) -> collectionValue.forEach(value -> {
            String target = this.getTargetName(columnName, value);
            dsIndex.add(target);
        }));

        return dsIndex;
    }

    /**
     * 获取数据源/表，最终路由目标
     * @param columnName
     * @param value
     * @return
     */
    public abstract String getTargetName(String columnName, String value);
}
